package com.ktgj.service.controller;

import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.service.domain.Contract;
import com.ktgj.service.domain.ContractLinkBoarding;
import com.ktgj.service.domain.ContractLinkBoardingPrice;
import com.ktgj.service.domain.Vipboarding;
import com.ktgj.service.service.IContractService;
import com.ktgj.service.service.IVipboardingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 值机服务Controller
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/vipboarding")
public class VipboardingController extends BaseController
{
    @Autowired
    private IVipboardingService vipboardingService;

    @Autowired
    private IContractService contractService;

    /**
     * 查询值机服务列表
     */
    //@PreAuthorize(hasPermi = "service:vipboarding:list")
    @GetMapping("/list")
    public TableDataInfo list(Vipboarding vipboarding)
    {
        startPage();
        List<Vipboarding> list=vipboardingService.selectVipboardingList(vipboarding);
        return getDataTable(list);
    }

    /**
     * 导出值机服务列表
     */
    //@PreAuthorize(hasPermi = "service:vipboarding:export")
    @Log(title = "值机服务 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Vipboarding vipboarding) throws IOException
    {
        List<Vipboarding> list = vipboardingService.selectVipboardingList(vipboarding);
        ExcelUtil<Vipboarding> util = new ExcelUtil<Vipboarding>(Vipboarding.class);
        util.exportExcel(response, list, "值机服务 数据");
    }

    /**
     * 获取值机服务详细信息
     */
    //@PreAuthorize(hasPermi = "service:vipboarding:query")
    @GetMapping(value = "/{boardingId}")
    public AjaxResult getInfo(@PathVariable("boardingId") Long boardingId)
    {
        return AjaxResult.success(vipboardingService.selectVipboardingById(boardingId));
    }

    /**
     * 新增值机服务
     */
    //@PreAuthorize(hasPermi = "service:vipboarding:add")
    @Log(title = "值机服务 ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Vipboarding vipboarding,Contract contract)
    {
            return toAjax(vipboardingService.insertVipboarding(vipboarding));
    }

    /**
     * 修改值机服务
     */
    //@PreAuthorize(hasPermi = "service:vipboarding:edit")
    @Log(title = "值机服务 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Vipboarding vipboarding)
    {
        return toAjax(vipboardingService.updateVipboarding(vipboarding));
    }

    /**
     * 删除值机服务
     */
    //@PreAuthorize(hasPermi = "service:vipboarding:remove")
    @Log(title = "值机服务 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{boardingIds}")
    public AjaxResult remove(@PathVariable Long[] boardingIds)
    {
        return toAjax(vipboardingService.deleteVipboardingByIds(boardingIds));
    }

    /**
     * 值机与合同多对多查询
     *
     * @param contractId 合同ID
     * @return 值机与合同集合
     * */
    @PostMapping("/lists")
    public AjaxResult getlist(Long contractId){
        return AjaxResult.success(vipboardingService.selectVipboardingByContractId(contractId));
    }

    /**
     * 新增值机合同、价格维护
     * @param linkBoardingPrice 值机合同价格对象
     * @return
     */
    @Log(title = "值机服务合同价格维护 ", businessType = BusinessType.INSERT)
    @PostMapping("/addVipBoardingContract")
    public AjaxResult addVipBoardingContract(@RequestBody ContractLinkBoardingPrice linkBoardingPrice){
        //TODO 接受前端数据
        int result = 0;
        List<ContractLinkBoarding> list = linkBoardingPrice.getLinkBoardingList();
        for (ContractLinkBoarding contractLinkBoarding : list) {
            ContractLinkBoarding boarding=new ContractLinkBoarding();
            boarding.setBoardingId(contractLinkBoarding.getBoardingId());
            boarding.setContractId(contractLinkBoarding.getContractId());
            //验证数据是否存在
            result = vipboardingService.selectBoardingContract(boarding.getContractId(),
                    boarding.getBoardingId());
        }
        if(result > 0){
            return AjaxResult.error("该服务项目价格已存在!");
        }
        //执行价格维护操作
        return toAjax(vipboardingService.addBoardingPrice(linkBoardingPrice));
    }

    /**
     * 值机  机场支持航司服务一对多查询
     * @param boardingId 贵宾厅信息ID
     * @return 值机  机场支持航司服务一对多查询集合
     * */
    @PostMapping("/Vipboarding")
    public AjaxResult selectVipboardingByBoardingId(Long boardingId){
        return AjaxResult.success(vipboardingService.selectVipboardingByBoardingId(boardingId));
    }

    /**
     * 获取值机列表，远程使用
     * @return
     */
    @GetMapping("/getVipboardingList")
    public List<Vipboarding> getVipboardingList(){
        List<Vipboarding> list = vipboardingService.getVipboardingList();
        return list;
    }

}
